package com.shinhye.elephantsafe.utils;

import com.shinhye.elephantsafe.R;
import com.shinhye.elephantsafe.SplashActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.BitmapFactory;

//TODO -9- 访问快捷方式数据库，不添加重复快捷方式
public class ShortcutUtils {

	/**
	 * Create a shortcut.
	 * @param context
	 * @param ignoreSetting - If true ignore the shared preferences setting.
	 */
	public static void create(Context context, boolean ignoreSetting) {
		String APPNAME = "白象卫士";
    	SharedPreferences sp = context.getSharedPreferences("Setting", Context.MODE_PRIVATE);
    	boolean setting = sp.getBoolean("shortcut", false);
    	if(ignoreSetting || !setting) {
    		Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    		//图标
    		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));
    		//图标名称
    		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, APPNAME);
    		//快捷图标要做什么？
    		Intent act_intent = new Intent(context, SplashActivity.class);
    		act_intent.setAction(Intent.ACTION_MAIN);
    		act_intent.addCategory(Intent.CATEGORY_LAUNCHER);
    		//添加要做什么事的意图到快捷图标意图
    		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, act_intent);
    		//发送广播
    		context.sendBroadcast(intent);
    		
    		Editor editor = sp.edit();
    		editor.putBoolean("shortcut", true);
    		editor.commit();
    	}

	}
}
